package checkers3d.storage;

import checkers3d.logic.*;
import checkers3d.presentation.GameEngine;
import java.io.*;
import java.util.*;

/**
 *
 * @author Sean Keel
 */
public class DataManagerGame{

    /**
     * The relative path to the location where saved games are stored.
     */
    static public final String LOCATION_SAVEGAMES = ".//save/";

    /**
     * The current player of the game that was found in a previously saved game.
     */
    static private Player currentPlayer;

    /**
     * Saves each state of the board after a move has been made by saving the
     * current player of the game and each board tile location, including which
     * BoardObject is located there and who owns that BoardObject.
     *
     * @param x The x coordinate from a Point3D.
     * @param y The y coordinate from a Point3D.
     * @param z The coordinate from a Point3D.
     * @param object The object that was stored at the Point3D location.
     * @param currentPlayer The name of the current player of the game.
     */
    static public void saveGameData(PlayingSurface3D playingSurface3D, String currentPlayer, String playerOneName, String playerTwoName) throws IOException{
        String filename = (".//save/" + playerOneName + "&" + playerTwoName + ".txt");
        DataManagerUtil.eraseFile(filename);

        FileWriter formattedFile = new FileWriter(filename, true);
        BufferedWriter out = new BufferedWriter(formattedFile);

        out.write("*\t" + currentPlayer);
        out.newLine();

        DataManagerUtil.writePlayingSurface3D(out, playingSurface3D);

        out.close();
        formattedFile.close();
    }

    /**
     * Loads the instant replay data from the InstantReplayData file.
     */
    static public PlayingSurface3D loadGameData(String filename, Player home, Player visitor) throws IOException{
        FileReader fr = new FileReader(filename);
        Scanner scan = new Scanner(fr);
        PlayingSurface3D ps3D;

        //set graphics for the home player.
        home.setCheckerRR("player1tokenchecker.png");
        home.setKingRR("player1tokenking.png");
        home.setWallRR("generictokenwall.png");
        home.setSafeRR("player1tokensafe.png");
        home.setMineRR("player1tokenmine.png");

        //set graphics for the visiting player.
        visitor.setCheckerRR("player2tokenchecker.png");
        visitor.setKingRR("player2tokenking.png");
        visitor.setWallRR("generictokenwall.png");
        visitor.setSafeRR("player2tokensafe.png");
        visitor.setMineRR("player2tokenmine.png");

        String line = scan.nextLine();
        String[] currentPlayerLine = line.split("\t");
        String currentPlayerName = currentPlayerLine[1];
        String currentPlayerPassword = DataManagerAccounts.getPlayerPassword(currentPlayerName);

        Player lastCurrentPlayer = new Player(currentPlayerName, currentPlayerPassword);

        if (lastCurrentPlayer.getName().equals(home.getName()))
            DataManagerGame.currentPlayer = home;
        else if (lastCurrentPlayer.getName().equals(visitor.getName()))
            DataManagerGame.currentPlayer = visitor;

        scan.close();
        FileReader reader = new FileReader(filename);
        Scanner scanner = new Scanner(reader);

        ps3D = DataManagerUtil.readPlayingSurface3D(scanner, home, visitor);
        GameMaster.getInstance().addGUIContainer(new Game(home, visitor, ps3D, GameEngine.WINDOW_SIZE));

        scan.close();
        fr.close();
        return ps3D;
    }

    static public Player getCurrentPlayer(){
        return DataManagerGame.currentPlayer;
    }
}